import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class LocalDBTestStub implements LocalDBInterface {
	private static final int NFIELDS = 100;
	private static ArrayList<Product> localDB;
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public boolean decrementCount(int productId, int count) {
		//Real object should decrement inventory count on this product by count
		//Since no expiration date is specified, the product with the lowest expiration
		//date should be decremented.
		return true;
	}

	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public Product getProduct(int productId) {
		Product ret = null;
		DBConnection conn = new DBConnection(Constants.DBType.EPRODUCTS);
		conn.connect();
		Map<String, String> m = new HashMap<String, String>();
        m.put("privateProducts.productId", Integer.toString(productId));
		Record results[] = conn.getRecords( m);
		if( results.length>0)
			ret = (Product)results[0];
		conn.disconnect();
		
		return ret;
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public static void populateDB() {
		int i;
		int j;
		String names[] = {	"Mayo",
							"Milk",
							"Rice",
							"Butter",
							"Shampoo",
							"Toothpaste",
							"Pillow",
							"Pencil",
							"Water Bottle",
							"Mince",
							"Burger",
							"Bread",
							"Hot dog bun",
							"Burger bun",
							"Coffee",
							"Cheese stick",
							"Fish stick",
							"Boardgame",
							"Trash bin",
							"Book"};
		
		Random randomGenerator = new Random();
		localDB = new ArrayList<Product>();
		Product newProduct;
		
		
		for( i=0, j=0; i<NFIELDS; i++)
		{
			newProduct = new Product();
			newProduct.setId(i);
			newProduct.setProductName(names[j++]);
			newProduct.setPrice(randomGenerator.nextInt(100)+randomGenerator.nextInt(100)/(float)100.0);
			localDB.add(newProduct);
			if( j>=names.length) j = 0;
		}
		
	}

}
